package com.aboverock.common.domain;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.aboverock.common.constant.KbsmartConstant;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.builder.ToStringBuilder;


/**
 * 分页组件datagrid、combogrid数据模型.
 *
 * @author Rock Wang
 * @param <T> the generic type
 */
public class Datagrid<T> implements Serializable {
    
    private static final long serialVersionUID = 6611358982350038892L;

    /** 列表行. */
    private List<T> records = new ArrayList<T>(0);

    /** 总记录数. */
    private long total = 0;

    /** 每页记录数. */
    private int size = KbsmartConstant.Pagination.PAGE_SIZE_DEFAULT;

    /** 当前页，页码从1开始. */
    private int current = KbsmartConstant.Pagination.CURRENT_PAGE_DEFAULT;
    
    /** 脚列表. */
    private List<Map<String, Object>> footer = new ArrayList<Map<String, Object>>(0);

    /**
     * Instantiates a new datagrid.
     */
    public Datagrid() {
        super();
    }

    /**
     * Instantiates a new datagrid.
     *
     * @param total 总记录数
     * @param size 每页记录数
     * @param current 当前页码
     * @param records 列表行
     */
    public Datagrid(long total, int size, int current, List<T> records) {
        this(total, size, current, records, null);
    }
    
    /**
     * Instantiates a new datagrid for mybatis-plus pagination.
     *
     * @param page the page
     */
    public Datagrid(Page<T> page) {
        this(page.getTotal(), (int) page.getSize(), (int) page.getCurrent(), page.getRecords(), null);
    }
    
    /**
     * Instantiates a new datagrid for spring data page.
     *
     * @param page the page
     */
    public Datagrid(org.springframework.data.domain.Page<T> page) {
        this(page.getTotalElements(), page.getPageable().getPageSize(), page.getPageable().getPageNumber() + 1, page
                .getContent(), null);
    }

    /**
     * Instantiates a new datagrid for spring-data pagination.
     *
     * @param total 总记录数
     * @param size 每页记录数
     * @param current 当前页码
     * @param records 列表行
     * @param footer 脚列表
     */
    public Datagrid(long total, int size, int current, List<T> records, List<Map<String, Object>> footer) {
        super();
        this.total = total;
        this.size = size;
        this.current = current;
        this.records = records;
        this.footer = footer;
    }

    /**
     * 总记录数.
     *
     * @return the total
     */
    public long getTotal() {
        return total;
    }

    /**
     * 设置总记录数.
     *
     * @param total 总记录数
     * @return the datagrid
     */
    public Datagrid<T> setTotal(long total) {
        this.total = total;
        return this;
    }
    
    /**
     * 返回每页记录数.
     *
     * @return the size
     */
    public int getSize() {
        return size;
    }

    /**
     * 设置每页记录数.
     *
     * @param size the size
     * @return the datagrid
     */
    public Datagrid<T> setSize(int size) {
        this.size = size;
        return this;
    }

    /**
     * 返回当前页码.
     *
     * @return the current
     */
    public int getCurrent() {
        return current;
    }

    /**
     * 设置当前页码.
     *
     * @param current the current
     * @return the datagrid
     */
    public Datagrid<T> setCurrent(int current) {
        this.current = current;
        return this;
    }

    /**
     * 列表行.
     *
     * @return the records
     */
    public List<T> getRecords() {
        return records;
    }

    /**
     * 设置列表行.
     *
     * @param records the records
     * @return the datagrid
     */
    public Datagrid<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    /**
     * 脚列表.
     *
     * @return the footer
     */
    public List<Map<String, Object>> getFooter() {
        return footer;
    }

    /**
     * 设置脚列表.
     *
     * @param footer 脚列表
     * @return the datagrid
     */
    public Datagrid<T> setFooter(List<Map<String, Object>> footer) {
        this.footer = footer;
        return this;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}
